import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class GemXMLParser extends DefaultHandler {

	private Board board;

	public GemXMLParser(String ficheroXML) {
		try {
			this.board = new Board();

			SAXParserFactory parserFactory = SAXParserFactory.newInstance();
			parserFactory.setValidating(true);
			parserFactory.setNamespaceAware(true);
			SAXParser parser = parserFactory.newSAXParser();
			parser.parse(ficheroXML, this);
		} catch (Exception ex) {
			System.err.println(this.getClass().getName() + ": "
					+ ex.getMessage());
		}
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		String color;
		int weight;

		if (qName.equals("is:gem")) {
			color = attributes.getValue("color");
			weight = Integer.parseInt(attributes.getValue("weight"));
			this.board.addElementToDomain(new Gem(color, weight));
		}
	}

	public Board getTablero() {
		return this.board;
	}
}
